package com.villaday.idf.util;

import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.core.graphics.drawable.DrawableCompat;
import android.widget.ImageView;

public class ImageViewUtils {
    //drawable 着色
    public static void setImageViewColor(ImageView view, int colorResId) {
        //mutate()
        Drawable modeDrawable = view.getDrawable().mutate();
        Drawable temp = DrawableCompat.wrap(modeDrawable);
        ColorStateList colorStateList =     ColorStateList.valueOf(colorResId);
        DrawableCompat.setTintList(temp, colorStateList);
        view.setImageDrawable(temp);
    }


    /**
     * 颜色渐变
     *
     * @param startColor 起始颜色
     * @param endColor 终止颜色
     * @param ratio 颜色变化频率 从0-1
     * @return 颜色值
     */
    public static int blendColors(int startColor, int endColor, float ratio) {
        float inverseRatio = 1.0F - ratio;
        float a = (float) Color.alpha(startColor) * inverseRatio + (float) Color.alpha(endColor) * ratio;
        float r = (float) Color.red(startColor) * inverseRatio + (float) Color.red(endColor) * ratio;
        float g = (float) Color.green(startColor) * inverseRatio + (float) Color.green(endColor) * ratio;
        float b = (float) Color.blue(startColor) * inverseRatio + (float) Color.blue(endColor) * ratio;
        return Color.argb((int) a, (int) r, (int) g, (int) b);
    }



    private Drawable getTintDrawable(Drawable drawable,@ColorInt int color) {
        Drawable.ConstantState state = drawable.getConstantState();
        Drawable drawable1 = DrawableCompat.wrap(state == null ? drawable : state.newDrawable()).mutate();
        drawable1.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        DrawableCompat.setTint(drawable1, color);
        return drawable1;
    }

    /**
     *
     * @param drawable
     * @param colors
     * @param states
     * @return
     *
     * int[] colors = new int[] { ContextCompat.getColor(this,R.color.pink),ContextCompat.getColor(this,R.color.pink1)};
     *         int[][] states = new int[2][];
     *         states[0] = new int[] { android.R.attr.state_pressed};
     *         states[1] = new int[] {};
     */
    private Drawable getStateDrawable(Drawable drawable, int[] colors, int[][] states) {
        ColorStateList colorList = new ColorStateList(states, colors);

        Drawable.ConstantState state = drawable.getConstantState();
        drawable = DrawableCompat.wrap(state == null ? drawable : state.newDrawable()).mutate();
        DrawableCompat.setTintList(drawable,colorList);
        return drawable;
    }

    @NonNull
    private StateListDrawable getStateListDrawable(Drawable drawable, int[][] states) {
        StateListDrawable stateListDrawable = new StateListDrawable();
        for (int[] state:states) {
            stateListDrawable.addState(state,drawable);
        }
        return stateListDrawable;
    }

}
